Skip to content
This repository was archived by the owner on Apr 2, 2026. It is now read-only.

Add map_err_with()#951

Merged
zesterer merged 2 commits into
zesterer:mainfrom
tmke8:map-err-with
Jan 23, 2026
Merged

Add map_err_with()#951
zesterer merged 2 commits into
zesterer:mainfrom
tmke8:map-err-with

Conversation

@tmke8

@tmke8 tmke8 commented Jan 19, 2026

Copy link
Copy Markdown
Contributor

closes #948

See discussion in the linked issue.

@tmke8 tmke8 mentioned this pull request Jan 19, 2026
Comment thread src/lib.rs
Comment on lines +2000 to +2007
/// Map the primary error of this parser to another value, making use of the parser state and
/// context.
///
/// This function is useful for augmenting errors to allow them to include context in non context-free
/// languages, or provide contextual notes on possible causes.
///
/// The output type of this parser is `O`, the same as the original parser.
///

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I think this change is a positive, I am a bit worried about users relying on things like MapExtra::slice, MapExtra::span, etc. Chumsky deliberately permits parsers to leave the input in an unspecified state after an error occurs and backtracking begins (it's up to calling parsers to explicitly rewind the input if they need recovery). As such, I think we should explicitly document that these functions may not produce the output a user might expect.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, while implementing this I was wondering what to provide to the closure. So I think you're saying that all of MapExtra is too much because some of it doesn't make much sense in case of an error?

Just span (map_error_with_state gets this at the moment though maybe computed differently), state and context then? Should it be a new struct or just passed individually to the closure? If it's the latter, this new function would be basically like the existing map_error_with_state but with one additional argument.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm honestly not sure! For now, I think the easiest approach (and most consistent with the rest of the crate) is to stick with MapExtra, but document that the output of MapExtra::span and MapExtra::slice are unspecified. This certainly warrants further thought, but I don't think it should block this PR. I'll merge when the change is made to the docs, thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a warning. Let me know if I should change the wording.

Should I also add a deprecation note for map_err_with_state()?

@zesterer zesterer left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment above. I am broadly in favour, I just think we need to nail down exactly what is and isn't specified :)

@zesterer

Copy link
Copy Markdown
Owner

Thanks!

@zesterer
zesterer merged commit b81c34c into zesterer:main Jan 23, 2026
4 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Map error with context?

2 participants